﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Management;

namespace Alex.Library.IO
{
    /// <summary>
    /// 文件操作Helper类
    /// </summary>
    public sealed class FileHelper
    {
        /// <summary>
        /// 拷贝文件
        /// </summary>
        /// <param name="src">源文件</param>
        /// <param name="dest">目标文件</param>
        /// <returns></returns>
        public static bool CopyFile(string src, string dest)
        {
            if (File.Exists(src))
            {
                File.Copy(src, dest, true);
            }

            return true;
        }
        /// <summary>
        /// 从一个目录下完整拷贝到另一个目录下。
        /// </summary>
        /// <param name="srcdir">源目录</param>
        /// <param name="destdir">目标目录</param>
        public static void CopyFiles(string srcdir, string destdir)
        {
            if (!Directory.Exists(srcdir))
            {
                return;
            }

            if (Directory.Exists(destdir))
            {
                Directory.CreateDirectory(destdir);
            }

            string[] directories = Directory.GetDirectories(srcdir);//取文件夹下所有文件夹名，放入数组；
            if (directories.Length > 0)
            {
                foreach (string d in directories)
                {
                    CopyFiles(d, destdir + d.Substring(d.LastIndexOf("\\")));//递归拷贝文件和文件夹
                }
            }

            string[] files = Directory.GetFiles(srcdir);//取文件夹下所有文件名，放入数组；
            if (files.Length > 0)
            {
                foreach (string s in files)
                {
                    XCopyFile(s, destdir);
                }
            }
        }
        /// <summary>
        /// 用XCopy命令拷贝文件
        /// </summary>
        /// <param name="srcDir">源文件</param>
        /// <param name="destDir">目标文件</param>
        public static void XCopyFile(string srcDir, string destDir)
        {
            try
            {
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.FileName = "cmd.exe ";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;
                p.Start();
                p.StandardInput.WriteLine(@"xcopy  " + srcDir + "   " + destDir + "\\*.* /s/e/y/h");
                p.StandardInput.WriteLine(@"exit ");
                p.StandardOutput.ReadToEnd();
                p.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="path">文件路径</param>        
        public static void DeleteFile(string path, bool throwExceptionIfNotExists = false)
        {
            if (throwExceptionIfNotExists)
            {
                if (File.Exists(path))
                {
                    throw new FileNotFoundException();
                }
            }
            try
            {
                File.Delete(path);
            }
            catch
            {
                throw;
            }
        }       
        /// <summary>
        /// 判断文件是否存在
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        public static bool Exists(string filePath)
        {
            return File.Exists(filePath);
        }
        /// <summary>
        /// 判断文件是否存在
        /// </summary>
        /// <param name="path">文件存在的目录路径</param>
        /// <param name="fileName">文件名（不包含路径）</param>
        /// <returns></returns>
        public static bool Exists(string path, string fileName)
        {
            return Exists(Path.Combine(path, fileName));
        }
        /// <summary>
        /// 获取文件大小
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <returns>文件大小 Bytes</returns>
        public static long GetFileSize(string fileName)
        {
            System.IO.FileInfo fi = new System.IO.FileInfo(fileName);
            return fi.Length;
        }
    }
}
